package com.example.demo.actuator.endpoint;

import com.example.demo.gateway.entity.GatewayUpstream;
import com.example.demo.gateway.filter.UpstreamReactorLoadBalancer;
import com.example.demo.gateway.repository.MySQLRouteDefinitionRepository;
import com.example.demo.gateway.service.GatewayRouteService;
import com.example.demo.gateway.service.GatewayUpstreamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

@Component
@Endpoint(id = "upstream")
public class GatewayUpstreamEndpoint {
    @Autowired
    private UpstreamReactorLoadBalancer upstreamReactorLoadBalancer;
    @Autowired
    private GatewayUpstreamService gatewayUpstreamService;

    @ReadOperation
    public Map<String, List<GatewayUpstream>> list() {
        return  upstreamReactorLoadBalancer.getUpstreams();
    }


    @WriteOperation
    public Mono<Void> refresh() {
        return gatewayUpstreamService.refresh();
    }
}
